/* eslint-disable */
/**
 * Created by GYL on 2018/8/24
 */
//登录页面(测试阶段)
import React, { Component } from 'react';
import { connect } from 'dva';
import { Row, Col, Input, message, Icon,Modal } from 'antd';
import request from '../../utils/request';
import md5 from 'blueimp-md5';
import PublicService from '../../services/PublicService';
import styles from './login.less';
import 'antd/dist/antd.css';
import Base64 from 'base-64';
const { confirm } = Modal;

class Login extends Component {
  state = {
    enter: false,
    user: '',
    pwd: '',
    code: '',
    codeImg: '',
    autoLogin: false,
    loading: false,
    codeUrl: '/pm-web/projectmanage/login/getSignCode?t=',
    inputType: 'password'
  };

  componentDidMount() {
    // console.log(this.props, "url")
    this.tu = (e) => {
      let tu = document.getElementById("tu")
      tu.style.display = "none"



    }
    // request(`/hh-management-web/hehui/stock/findAllPackingTypes`)
    let t = this;
    t.refreshCode();
    // console.log(12)
    // PublicService.clearCookie('nickName');
    // 获取自动登录ID
    // let autoLoginId = PublicService.getCookie('rememberMeId');
    // 自动登录ID存在 则自动登录
    // if (autoLoginId) {
    //   t.setState({
    //     autoLogin: true,
    //   });
    //   request({
    //     url: '/hh-management-web/hehui/login/login',
    //     method: 'POST',
    //     data: {
    //       rememberMeId: autoLoginId,
    //       rememberMe: true
    //     }
    //   }).then(
    //     data => {
    //       console.log(data, 'data');
    //       t.checkLogin(data);
    //     }
    //   );
    // }
  }


  // input change事件
  getUserInfo(inputIndex, e) {
    let t = this;
    if (inputIndex === 'autoLogin') {
      t.setState({
        autoLogin: e.target.checked
      })
    } else {
      t.setState({
        [inputIndex]: e.target.value.trim()
      });
    }
  }

  // 登录按钮
  login = () => {

    let t = this;
    if (this.state.user == '') {
      message.warning('请输入用户名');
      return
    }
    if (this.state.pwd == '') {
      message.warning('请输入密码');
      return
    }
    if (this.state.code == '') {
      message.warning('请输入验证码');
      return
    }
    t.setState({
      loading: true
    });
    let params = {
      password: md5(this.state.pwd),
      userName: this.state.user,
      v_verifyCode: t.state.code,
    };

    request({
      url: `/pm-web/projectmanage/login/login`,
      method: 'POST',
      data: params

    }).then(data => {
      // console.log(data)
      // this.checkLogin(data);
      if (data.rc == 0) {
        if (data.ret.loggingStatus == 1) {
          confirm({
            title: '登录提示!',
            content: '该账号已有用户登录,是否重复登录?',
            okText: '确定',
            cancelText: '取消',
            onOk:() => {
              window.sessionStorage.removeItem('panes')
              // this.props.history.push("/App")
    
              t.checkLogin(data);
            },
            onCancel:() => {
              let t = this;
              this.setState({
                loading: false
              });
              this.refreshCode();
            },
          });
        } else {
          window.sessionStorage.removeItem('panes')
          // this.props.history.push("/App")

          t.checkLogin(data);
        }


      }else{
        let t = this;
        this.setState({
          loading: false
        });
        this.refreshCode();
        message.warning(data.err);
      }
    }).catch((err) => {
      // console.log(err)
    })


    // requestJson({url: '/hh-management-web/gnhj/login/login', method: 'POST', data:params}).then(
    //   (data) => {
    //     console.log(document.cookie, '接口后cookie');
    //     t.checkLogin(data);
    //   }
    // ).catch(() => {
    //   message.warning('登录失败');
    //   t.setState({
    //     loading: false
    //   });
    // })

  }

  // 判断是否登录成功
  checkLogin(data) {
    // console.log(1)
    let t = this;
    this.setState({
      loading: false
    });
    this.refreshCode();
    // console.log(data)
    if (data.rc == 1) {

      // document.exitFullscreen ? document.exitFullscreen() :
      //   document.mozCancelFullScreen ? document.mozCancelFullScreen() :
      //     document.webkitExitFullscreen ? document.webkitExitFullscreen() : '';
      message.warning(data.err);
    } else {

      // 保存用户登录信息
      // t.props.dispatch({type: 'login/saveUser', payload: {user: data.ret}});
      PublicService.setCookie('userId', data.ret.userId);
      PublicService.setCookie('userName', data.ret.userName);
      PublicService.setCookie('nickName', data.ret.realName);
      PublicService.setCookie('logo', 2);

      window.location.href = `http://op.bgechina.cn:8100/gngc-web/index.html#/vortexLoginFromEnvRepairPlatform?ssoUserName=${Base64.encode(data.ret.userName)}`
      // this.props.history.push("/App")
      // message.success('登录成功');

    }
  }

  // 刷新验证码
  refreshCode() {
    let t = this;
    t.setState({
      codeUrl: t.state.codeUrl + new Date().getTime()
    });
  }

  // 显示密码
  pwdShow() {
    let t = this;
    t.setState({
      inputType: t.state.inputType.length === 0 ? 'password' : ''
    })
  }


  // 键盘事件
  keyUp(e) {
    let t = this;
    let code = e.charCode || e.keyCode;  //取出按键信息中的按键代码(大部分浏览器通过keyCode属性获取按键代码，但少部分浏览器使用的却是charCode)
    if (code === 13) {
      t.login();
    }
  }

  //返回按钮事件
  goBack = () => {

    let tu = document.getElementById("tu")
    tu.style.display = "inline"

  }

  videoPlay1 = () => {
    this.props.history.push("/video1")
  }

  onbackcc = () => {
    window.location.href = 'http://192.168.0.101:8000/#/super?name=xiaoming&password=123456'
  }




  render() {
    let t = this;
    let { loading } = t.state;
    return (
      <div className={styles["bg"]} onKeyUp={(e) => t.keyUp(e)}>
        <div className={styles.titlebox}>
          <img src={require("../../assets/titlelogo.png")} style={{ width: 70, height: 70, opacity: 1 }} />
          <div className={styles.text}>
            高能环境修复大脑平台
            {/* <span onClick={this.onbackcc}>跳过登录</span> */}

          </div>

        </div>

        <div className={styles.bottombox}>
          <div className={styles.bottomBox_text}>版权所有 : 北京高能时代环境技术股份有限公司</div>
          <div className={styles.bottomBox_text}>技术支持 : 苏州市伏泰信息科技股份有限公司</div>
        </div>

        {/* <img src={bjt} alt="" className={styles["zz"]} onClick={this.tu} ref="tu" id="tu"/> */}
        <div className={styles['wp-content']}>

          <div className={styles['wp-right']}>
            <div className={styles.clire}>
              <img src={require("../../assets/zf.png")} style={{ width: '8vh', height: '8vh', opacity: 1 }} />
            </div>
            <div>
              <p className={styles.titleText}>用户名 : </p>
              <Input className={styles.item_name} value={t.state.user} onChange={t.getUserInfo.bind(t, 'user')} />
            </div>
            <div>
              <p className={styles.titleText}>密&nbsp;&nbsp;&nbsp;码 : </p>
              <Input.Password size="small" type={t.state.inputType} className={styles.item_name} onChange={t.getUserInfo.bind(t, 'pwd')} />
            </div>



            <div className={styles.codel}>
              <div onClick={t.render.bind(t)} className={styles.boxice}>
                <p className={styles.titleText}>验证码 : </p>
                <img style={{ height: '4vh', width: '80%' }} onClick={t.refreshCode.bind(t)} src={t.state.codeUrl} />
              </div>
              <div className={styles.boxice}>
                <p>&nbsp;&nbsp;&nbsp;</p>
                <Input

                  className={styles.item_name}
                  // style={{ width: 100 }}
                  style={{ marginBottom: 0, height: '4vh', paddingLeft: '4%', boxSizing: 'border-box' }}
                  // className={styles['wp-login-yanzheng']}
                  placeholder="验证码"
                  onChange={t.getUserInfo.bind(t, 'code')} />

              </div>

            </div>


            <div
              onClick={t.login.bind(t)}
              className={styles['wp-login-btn']}
            >
              登录
                </div>
          </div>
          {
            loading &&
            <div className={styles['wp-loading']}></div>
          }
        </div>
      </div>
    );
  }
}
function mapStateToProps(state) {
  return {}
}
export default connect(mapStateToProps)(Login)
